using System.Text.RegularExpressions;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;

public class DialogueManager : MonoBehaviour
{
    public Text dialogueText;
    public Button laughButton;
    public Button doNothingButton;
    public Button continueButton; // New button for continuing to the next string

    public DialogueData dialogueData; // Reference to the ScriptableObject
    private int currentLineIndex = 0;
    private bool finished = false;
    private NPCInteraction _npcInteraction;

    public Text name;
    public GameObject matchVFX;
    public AudioSource laughterSource;
    public AudioClip[] laughterClips;

    void Start()
    {
        if (dialogueData != null && dialogueData.dialogueLines.Length > 0)
        {
            dialogueData.dialogueLines.CopyTo(dialogueData.dialogueLines, 0);
        }
        else
        {
            dialogueData = ScriptableObject.CreateInstance<DialogueData>();
            dialogueData.dialogueLines = new string[9];
        }

        _npcInteraction = gameObject.transform.parent.parent.gameObject.GetComponent<NPCInteraction>();
        
        // Add onClick listeners to the buttons
        laughButton.onClick.AddListener(OnlaughButtonClick);
        doNothingButton.onClick.AddListener(OndoNothingButtonClick);
        continueButton.onClick.AddListener(OnContinueButtonClick);

        DisplayCurrentLine();
        DisableButtons();
        //currentLineIndex++;
    }

    void DisplayCurrentLine()
    {
        if (currentLineIndex < dialogueData.dialogueLines.Length)
        {
            dialogueText.text = dialogueData.dialogueLines[currentLineIndex];
            
            /*
            if (currentLineIndex % 2  != 0 )//&& currentLineIndex != 0)
            {
                EnableButtons();
            }
            else
            {
                DisableButtons();
            }
            */


            // Show the continue button if a specific condition (e.g., bool) is true
            //continueButton.gameObject.SetActive(dialogueData.dialogueLikes[currentLineIndex]);
        }
        else
        {
            //CloseDialogue();
        }
    }

    void EnableButtons()
    {
        laughButton.gameObject.SetActive(true);
        doNothingButton.gameObject.SetActive(true);
        continueButton.gameObject.SetActive(false);
        name.text = dialogueData.name;
    }

    void DisableButtons()
    {
        laughButton.gameObject.SetActive(false);
        doNothingButton.gameObject.SetActive(false);
        continueButton.gameObject.SetActive(true);
        if (finished)
        {
            name.text = dialogueData.name;
            continueButton.gameObject.transform.GetChild(1).gameObject.SetActive(false);
            continueButton.gameObject.transform.GetChild(2).gameObject.SetActive(true);
        }
       else name.text = "me";

    }

    void OnlaughButtonClick()
    {
        if (currentLineIndex < dialogueData.dialogueLikes.Length && dialogueData.dialogueLikes[currentLineIndex])
        {

            if (currentLineIndex < dialogueData.dialogueLines.Length - 1)
            {
                
                currentLineIndex++;
                DisplayCurrentLine();
                
            }
            else
            {
                finished = true;
                dialogueText.text = dialogueData.didLaugh;
            }
        }
        else
        {
            finished = true;
            dialogueText.text = dialogueData.didLaugh;
        }
        laughterSource.clip = laughterClips[Random.Range(0, laughterClips.Length)];
        laughterSource.Play();
        DisableButtons();

    }

    void OndoNothingButtonClick()
    {
        if (currentLineIndex < dialogueData.dialogueLikes.Length && !dialogueData.dialogueLikes[currentLineIndex])
        {
            if (currentLineIndex < dialogueData.dialogueLines.Length - 1)
            {
                currentLineIndex++;
                DisplayCurrentLine();
            }
            else
            {
                finished = true;
                dialogueText.text = dialogueData.didntLaugh;
            }
        }
        else
        {
            finished = true;
            dialogueText.text = dialogueData.didntLaugh;
        }
        DisableButtons();
    }

    void OnContinueButtonClick()
    {
        if (finished || currentLineIndex == dialogueData.dialogueLines.Length - 1)
        {
            CloseDialogue();
        }
        // Continue to the next string
        else if (currentLineIndex < dialogueData.dialogueLines.Length - 1)
        {
            currentLineIndex++;
            DisplayCurrentLine();
            EnableButtons();
        }
    }

    public void CloseDialogue()
    {
        currentLineIndex = 0;
        DisplayCurrentLine();
        DisableButtons();

        _npcInteraction.playerTalking.talking = false;
        _npcInteraction._audioSource.Stop();

        finished = false;
        continueButton.gameObject.transform.GetChild(1).gameObject.SetActive(true);
        continueButton.gameObject.transform.GetChild(2).gameObject.SetActive(false);
        gameObject.SetActive(false);
    }

    public void match()
    {
        matchVFX.SetActive(true);
            _npcInteraction = gameObject.transform.parent.parent.gameObject.GetComponent<NPCInteraction>();
        _npcInteraction.gameObject.GetComponent<NPCMovement>().enabled = false;
            _npcInteraction.gameObject.GetComponent<NPCStop>().enabled = false;
            _npcInteraction.gameObject.GetComponent<WrapAround>().enabled = false;
            _npcInteraction.gameObject.GetComponent<NPCFollow>().enabled = false;
            _npcInteraction.gameObject.GetComponent<NPCController>().enabled = false;
            _npcInteraction.gameObject.GetComponent<NavMeshAgent>().enabled = false;
            
    }
}